/*
 * CanvasEx.java
 *
 * @author Vladimir Krukov
 */

package jimm.ui.base;

import DrawControls.*;
import java.util.Vector;
import javax.microedition.lcdui.*;
import jimm.*;
import jimm.modules.*;
import jimm.util.JLocale;

/**
 * Basic class for UI-controls.
 *
 * @author Vladimir Kryukov
 */
abstract public class CanvasEx extends DisplayableEx {
    public static final byte FONT_STYLE_PLAIN = 0;
    public static final byte FONT_STYLE_BOLD  = 1;
    
    public static final byte THEME_BACKGROUND           = 0;
    public static final byte THEME_TEXT                 = 1;
    public static final byte THEME_CAP_BACKGROUND       = 2;
    public static final byte THEME_CAP_TEXT             = 3;
    public static final byte THEME_PARAM_VALUE          = 4;
    
    public static final byte THEME_CHAT_INMSG           = 5;
    public static final byte THEME_CHAT_OUTMSG          = 6;
    public static final byte THEME_CHAT_FROM_HISTORY    = 7;
    
    public static final byte THEME_CONTACT_ONLINE       = 8;
    public static final byte THEME_CONTACT_WITH_CHAT    = 9;
    public static final byte THEME_CONTACT_OFFLINE      = 10;
    public static final byte THEME_CONTACT_TEMP         = 11;
    
    public static final byte THEME_SCROLL_BACK          = 12;
    
    public static final byte THEME_SELECTION_RECT       = 13;
    public static final byte THEME_BACK                 = 14;
    
    public static final byte THEME_SPLASH_BACKGROUND    = 15;
    public static final byte THEME_SPLASH_LOGO_TEXT     = 16;
    public static final byte THEME_SPLASH_MESSAGES      = 17;
    public static final byte THEME_SPLASH_DATE          = 18;///
    public static final byte THEME_SPLASH_PROGRESS_BACK = 19;
    public static final byte THEME_SPLASH_PROGRESS_TEXT = 20;
    public static final byte THEME_SPLASH_LOCK_BACK     = 21;
    public static final byte THEME_SPLASH_LOCK_TEXT     = 22;
    
    public static final byte THEME_MAGIC_EYE_NUMBER     = 23;
    public static final byte THEME_MAGIC_EYE_ACTION     = 24;
    public static final byte THEME_MAGIC_EYE_NL_USER    = 25;
    public static final byte THEME_MAGIC_EYE_USER       = 26;
    public static final byte THEME_MAGIC_EYE_TEXT       = 27;
    
    //public static final byte THEME_MENU_SHADOW          = 28;
    public static final byte THEME_MENU_BACK            = 29;
    public static final byte THEME_MENU_BORDER          = 30;
    public static final byte THEME_MENU_TEXT            = 31;
    public static final byte THEME_MENU_SEL_BACK        = 32;
    public static final byte THEME_MENU_SEL_BORDER      = 33;
    public static final byte THEME_MENU_SEL_TEXT        = 34;
    
    //public static final byte THEME_POPUP_SHADOW         = 35;
    public static final byte THEME_POPUP_BORDER         = 36;
    public static final byte THEME_POPUP_BACK           = 37;
    public static final byte THEME_POPUP_TEXT           = 38;

    public static final byte THEME_GROUP                = 39;
    public static final byte THEME_CHAT_HIGHLIGHT_MSG   = 40;
    public static final byte THEME_SELECTION_BACK       = 41;
    public static final byte THEME_CONTACT_STATUS       = 42;
    public static final byte THEME_PROTOCOL             = 43;
    public static final byte THEME_PROTOCOL_BACK        = 44;
    
    public static final byte THEME_FORM_EDIT            = 45;
    public static final byte THEME_FORM_TEXT            = 46;
    public static final byte THEME_FORM_BORDER          = 47;
    public static final byte THEME_FORM_BACK            = 48;
    
    // Width of scroller line
    protected final static int scrollerWidth = getScrollWidth();
    public static final int minItemHeight = getMinItemHeight();
    public static final int minItemWidth = getMinItemWidth();

    // Key event type
    public final static int KEY_PRESSED = 1;
    public final static int KEY_REPEATED = 2;
    public final static int KEY_RELEASED = 3;

    private boolean repaintLocked = false;
    private String[] softLabels = new String[3];
    private boolean directKey = false;
    
    
    private static int getMinItemHeight() {
        // #sijapp cond.if modules_TOUCH is "true"#
        NativeCanvas nc = NativeCanvas.getInstance();
        if (nc.hasPointerEvents()) {
            Font smallFont = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN,  Font.SIZE_SMALL);
            int iSize = Options.getInt(Options.OPTION_INTERFACE_SCHEME); //interface size
            if (iSize < 1){                                              //interface size
            return smallFont.getHeight() * 1;                            //interface size
            }                                                            //interface size
            if (iSize == 1){                                             //interface size
            return smallFont.getHeight() * 2;                            //interface size
            }                                                            //interface size
            if (iSize > 1){                                              //interface size
            return smallFont.getHeight() * 3;                            //interface size
            }                                                            //interface size
        }
        // #sijapp cond.end #
        return 0;
    }
    private static int getMinItemWidth() {
        NativeCanvas nc = NativeCanvas.getInstance();
        return Math.min(nc.getHeight(), nc.getWidth()) * 30 / 100;
    }
    private static int getScrollWidth() {
        int zoom = 2;
        NativeCanvas nc = NativeCanvas.getInstance();
        // #sijapp cond.if target is "MIDP2"#
        if (nc.hasPointerEvents()) {
            zoom = 5;
        }
        // #sijapp cond.end#
        return Math.max(nc.getMinScreenMetrics() * zoom / 100, 6);
    }

    protected final int getScreenWidth() {
        return NativeCanvas.getScreenWidth();
    }
    protected final int getScreenHeight() {
        if (isSoftBarShown()) {
            return NativeCanvas.getScreenHeight() - NativeCanvas.getSoftBarHeight();
        }
        return NativeCanvas.getScreenHeight();
    }
    public boolean isSoftBarShown() {
        // #sijapp cond.if modules_TOUCH isnot "true"#
        if (!Options.getBoolean(Options.OPTION_SHOW_SOFTBAR)) {
            return false;
        }
        // #sijapp cond.end#
        return !(this instanceof jimm.SplashCanvas);
    }

    /**
     * UI dinamic update
     */
    protected void updateTask() {
    }

    
    protected int[] getScroll() {
        return null;
    }
    // #sijapp cond.if modules_TOUCH is "true"#
    protected void setScrollTop(int top) {
    }
    protected int getScrollTop() {
        return 0;
    }
    protected void stylusPressed(int x, int y) {
    }
    protected void stylusXMoved(int fromX, int fromY, int toX, int toY) {
    }
    protected void stylusXMoving(int fromX, int fromY, int toX, int toY) {
    }
    protected void stylusTap(int x, int y, boolean longTap) {
    }
    protected void stylusGeneralYMoved(int fromX, int fromY, int toX, int toY) {
    }
    protected final void stylusYMoving(int fromX, int fromY, int toX, int toY) {
        stylusGeneralYMoved(fromX, fromY, toX, toY);
    }
    protected final void stylusYMoved(int fromX, int fromY, int toX, int toY) {
        stylusGeneralYMoved(fromX, fromY, toX, toY);
    }
    protected final void stylusKineticYMoving(int fromX, int fromY, int toX, int toY) {
        stylusGeneralYMoved(fromX, fromY, toX, toY);
    }
    // #sijapp cond.end#

    /**
     * paint procedure
     */
    protected abstract void paint(GraphicsEx g);
    
    protected final void paintBack(GraphicsEx g) {
        if (this != Jimm.getJimm().getDisplay().getCurrentDisplay()) {
            return;
        }
        try {
            Vector v = jimm.Jimm.getJimm().getDisplay().getStack();
            int from = 0;
            for (int i = v.size() - 1; 0 <= i; --i) {
                if (v.elementAt(i) instanceof VirtualList) {
                    from = i;
                    break;
                } else if (!(v.elementAt(i) instanceof CanvasEx)) {
                    from = i;
                    break;
                }
            }
            for (int i = from; i < v.size(); ++i) {
                Object o = v.elementAt(i);
                if (o instanceof CanvasEx) {
                    ((CanvasEx)o).paint(g);

                } else {
                    g.setThemeColor(THEME_BACK);
                    g.fillRect(g.getClipX(), g.getClipY(), g.getClipWidth(), g.getClipHeight());
                }
            }
        } catch (Exception e) {
            // #sijapp cond.if modules_DEBUGLOG is "true" #
            DebugLog.panic("paint back", e);
            // #sijapp cond.end #
        }
        g.clipRect(0, 0, getScreenWidth(), getScreenHeight());
    }

        
    
    // protected void invalidate()
    public final void invalidate() {
        if (repaintLocked) return;
        NativeCanvas.getInstance().invalidate(this);
    }
    
    public final void lock() {
        repaintLocked = true;
    }
    
    public final void unlock() {
        repaintLocked = false;
        invalidate();
    }
    
    protected final boolean isLocked() {
        return repaintLocked;
    }
    
    protected void doKeyReaction(int keyCode, int actionCode, int type) {
    }
    protected boolean qwertyKey(int keyCode, int type) {
        return false;
    }
    
    public final void setSoftBarLabels(String more, String ok, String back, boolean direct) {
        softLabels[0] = JLocale.getString(more); // menu
        softLabels[1] = JLocale.getString(ok);   // default
        softLabels[2] = JLocale.getString(back); // back
        directKey = direct;
        NativeCanvas.getInstance().updateSoftBarHeight();
    }
    final boolean isNotSwappable() {
        return directKey;
    }
    final boolean hasRightSoft() {
        return softLabels[0] != softLabels[1];
    }
    final boolean isSwapped() {
        return softLabels[1] == softLabels[2];
    }
    final String[] getSoftLabels() {
        return softLabels;
    }
}